home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Direct3D / MFCPixelShader / pixelshader.h < prev    next >
C/C++ Source or Header  |  2001-10-31  |  10KB  |  312 lines

  1. //-----------------------------------------------------------------------------
  2. // File: D3DApp.h
  3. //
  4. // Desc: Header file for a D3DIM app that uses MFC
  5. //
  6. //
  7. // Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #ifndef D3D_MFC_APP_H
  10. #define D3D_MFC_APP_H
  11.  
  12. #if _MSC_VER >= 1000
  13. #pragma once
  14. #endif
  15. #ifndef __AFXWIN_H__
  16. #error include 'stdafx.h' before including this file
  17. #endif
  18.  
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Name: class CAppDoc
  25. // Desc: Overridden CDocument class needed for the CFormView
  26. //-----------------------------------------------------------------------------
  27. class CAppDoc : public CDocument
  28. {
  29. protected:
  30.     DECLARE_DYNCREATE(CAppDoc)
  31.  
  32. // Overrides
  33.     // ClassWizard generated virtual function overrides
  34.     //{{AFX_VIRTUAL(CAppDoc)
  35.     public:
  36.     //}}AFX_VIRTUAL
  37.  
  38. // Implementation
  39.     //{{AFX_MSG(CAppDoc)
  40.         // NOTE - the ClassWizard will add and remove member functions here.
  41.         //    DO NOT EDIT what you see in these blocks of generated code !
  42.     //}}AFX_MSG
  43.     DECLARE_MESSAGE_MAP()
  44. };
  45.  
  46.  
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Name: class CAppFrameWnd
  51. // Desc: CFrameWnd-based class needed to override the CFormView's window style
  52. //-----------------------------------------------------------------------------
  53. class CAppFrameWnd : public CFrameWnd
  54. {
  55. protected:
  56.     DECLARE_DYNCREATE(CAppFrameWnd)
  57.     // ClassWizard generated virtual function overrides
  58.     //{{AFX_VIRTUAL(CAppFrameWnd)
  59.     public:
  60.     virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
  61.     //}}AFX_VIRTUAL
  62.  
  63. protected:
  64.     //{{AFX_MSG(CAppFrameWnd)
  65.     //}}AFX_MSG
  66.     DECLARE_MESSAGE_MAP()
  67. };
  68.  
  69.  
  70.  
  71.  
  72. //-----------------------------------------------------------------------------
  73. // Name: class CApp
  74. // Desc: Main MFCapplication class derived from CWinApp.
  75. //-----------------------------------------------------------------------------
  76. class CApp : public CWinApp
  77. {
  78. public:
  79.  
  80. // Overrides
  81.     // ClassWizard generated virtual function overrides
  82.     //{{AFX_VIRTUAL(CApp)
  83.     public:
  84.     virtual BOOL InitInstance();
  85.     virtual BOOL OnIdle( LONG );
  86.     //}}AFX_VIRTUAL
  87.  
  88. // Implementation
  89.     //{{AFX_MSG(CApp)
  90.     //}}AFX_MSG
  91.     DECLARE_MESSAGE_MAP()
  92. };
  93.  
  94.  
  95.  
  96.  
  97. //-----------------------------------------------------------------------------
  98. // Name: class CAppForm
  99. // Desc: CFormView-based class which allows the UI to be created with a form
  100. //       (dialog) resource. This class manages all the controls on the form.
  101. //-----------------------------------------------------------------------------
  102. class CAppForm : public CFormView, public CD3DApplication
  103. {
  104.     HWND                    m_hwndRenderWindow;
  105.     HWND                    m_hwndRenderFullScreen;
  106.     LPDIRECT3DTEXTURE8      m_pTexture0;
  107.     LPDIRECT3DTEXTURE8      m_pTexture1;
  108.     LPDIRECT3DVERTEXBUFFER8 m_pQuadVB;
  109.     DWORD                   m_hPixelShader;
  110.     LPD3DXBUFFER            m_pD3DXBufShader;
  111.  
  112.     VOID UpdateUIForInstruction( INT iInstruction );
  113.     VOID    UpdateUIForDeviceCapabilites();
  114.     VOID    DisablePixelShaderEditingControls();
  115.     VOID    GeneratePixelShaderOpcodes();
  116.     HRESULT SetPixelShader( TCHAR* strOpcodes );
  117.     HRESULT ConfirmDevice( D3DCAPS8*,DWORD,D3DFORMAT );
  118.     HRESULT OneTimeSceneInit();
  119.     HRESULT InitDeviceObjects();
  120.     HRESULT RestoreDeviceObjects();
  121.     HRESULT FrameMove();
  122.     HRESULT Render();
  123.     HRESULT InvalidateDeviceObjects();
  124.     HRESULT DeleteDeviceObjects();
  125.     HRESULT FinalCleanup();
  126.     virtual HRESULT AdjustWindowForChange();
  127.  
  128. protected:
  129.     DECLARE_DYNCREATE(CAppForm)
  130.  
  131.              CAppForm();
  132.     virtual  ~CAppForm();
  133. public:
  134.     BOOL IsReady() { return m_bReady; }
  135.     TCHAR* PstrFrameStats() { return m_strFrameStats; }
  136.     VOID RenderScene() { Render3DEnvironment(); }
  137.     HRESULT CheckForLostFullscreen();
  138.  
  139.     //{{AFX_DATA(CAppForm)
  140.     enum { IDD = IDD_FORMVIEW };
  141.     //}}AFX_DATA
  142.  
  143.     //{{AFX_VIRTUAL(CAppForm)
  144.     virtual void OnInitialUpdate();
  145.     //}}AFX_VIRTUAL
  146.  
  147.     //{{AFX_MSG(CAppForm)
  148.     afx_msg void OnToggleFullScreen();
  149.     afx_msg void OnChangeDevice();
  150.     afx_msg void OnHScroll( UINT, UINT, CScrollBar* );
  151.     afx_msg void OnPresets();
  152.     afx_msg void OnOpenPixelShaderFile();
  153.     afx_msg void OnPixelShaderEdit();
  154.     afx_msg void OnChangeInstructions();
  155.     //}}AFX_MSG
  156.     DECLARE_MESSAGE_MAP()
  157.  
  158.  
  159.  
  160. };
  161.  
  162.  
  163.  
  164.  
  165. //-----------------------------------------------------------------------------
  166. // Pixel shader instructions
  167. //-----------------------------------------------------------------------------
  168. struct PIXELSHADER_OPCODE
  169. {
  170.     DWORD  opcode;
  171.     DWORD  dwNumArgs;
  172.     BOOL   bInstructionSupportsScaling;
  173.     BOOL   bInstructionSupportsBias;
  174.     BOOL   bInputsSupportModifiers;
  175.     BOOL   bUsesTextureRegisters;
  176.     TCHAR* optext;
  177.     TCHAR* description;
  178. };
  179.  
  180.  
  181. struct PIXELSHADER_PARAMETER
  182. {
  183.     DWORD  opcode;
  184.     TCHAR* optext;
  185.     TCHAR* description;
  186. };
  187.  
  188. #define D3DSIO_PS  0x00010000 // for specifying PixelShader version
  189.  
  190. // The pixel shader instructions
  191. PIXELSHADER_OPCODE g_PixelShaderInstructions[] =
  192. { //    opcode          numArgs Scaling Bias InputMods Tex  opText         Description
  193.     {   D3DSIO_NOP,          0, FALSE, FALSE, FALSE, FALSE, "nop",         "No operation" },
  194.     {   D3DSIO_PS,           2, FALSE, FALSE, FALSE, FALSE, "ps",          "Pixel Shader Version" },
  195.     {   D3DSIO_DEF,          5, FALSE, FALSE, FALSE, FALSE, "def",         "Define Constant" },
  196.     {   D3DSIO_MOV,          2,  TRUE,  TRUE,  TRUE, FALSE, "mov",         "Move" },
  197.     {   D3DSIO_ADD,          3,  TRUE,  TRUE,  TRUE, FALSE, "add",         "Add" },
  198.     {   D3DSIO_SUB,          3,  TRUE,  TRUE,  TRUE, FALSE, "sub",         "Subtract" },
  199.     {   D3DSIO_MAD,          4,  TRUE,  TRUE,  TRUE, FALSE, "mad",         "Multiply and add" },
  200.     {   D3DSIO_MUL,          3,  TRUE,  TRUE,  TRUE, FALSE, "mul",         "Multiply" },
  201.     {   D3DSIO_DP3,          3,  TRUE,  TRUE,  TRUE, FALSE, "dp3",         "DotProduct3" },
  202.     {   D3DSIO_LRP,          4,  TRUE,  TRUE,  TRUE, FALSE, "lrp",         "Interpolate (blend)" },
  203.     {   D3DSIO_CND,          4,  TRUE,  TRUE,  TRUE, FALSE, "cnd",         "Conditional" },
  204.     {   D3DSIO_TEXCOORD,     1, FALSE, FALSE, FALSE,  TRUE, "texcoord",    "Texture coord ???" },
  205.     {   D3DSIO_TEXKILL,      1, FALSE, FALSE, FALSE,  TRUE, "texkill",     "Texture kill ???" },
  206.     {   D3DSIO_TEX,          1, FALSE, FALSE, FALSE,  TRUE, "tex",         "Texture" },
  207.     {   D3DSIO_TEXBEM,       2, FALSE, FALSE, FALSE,  TRUE, "texbem",      "Bump env map" },
  208.     {   D3DSIO_TEXBEML,      2, FALSE, FALSE, FALSE,  TRUE, "texbeml",     "Bump env map w/luminance" },
  209.     {   D3DSIO_TEXREG2AR,    2, FALSE, FALSE, FALSE,  TRUE, "texreg2ar",   "???" },
  210.     {   D3DSIO_TEXREG2GB,    2, FALSE, FALSE, FALSE,  TRUE, "texreg2gb",   "???" },
  211.     {   D3DSIO_TEXM3x2PAD,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x2pad",  "3x2 ???" },
  212.     {   D3DSIO_TEXM3x2TEX,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x2tex",  "3x2 ???" },
  213.     {   D3DSIO_TEXM3x3PAD,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x3pad",  "3x3 ???" },
  214.     {   D3DSIO_TEXM3x3TEX,   2, FALSE, FALSE, FALSE,  TRUE, "texm3x3tex",  "3x3 ???" },
  215.     {   D3DSIO_TEXM3x3DIFF,  2, FALSE, FALSE, FALSE,  TRUE, "texm3x3diff", "3x3 ???" },
  216.     {   D3DSIO_TEXM3x3SPEC,  3, FALSE, FALSE, FALSE,  TRUE, "texm3x3spec", "3x3 ???" },
  217.     {   D3DSIO_TEXM3x3VSPEC, 2, FALSE, FALSE, FALSE,  TRUE, "texm3x3vspec","3x3 ???" },
  218.     {   D3DSIO_END,          0, FALSE, FALSE, FALSE, FALSE,  NULL,                NULL },
  219. };
  220.  
  221.  
  222.  
  223.  
  224. // The source parameters for the pixel shader instructions
  225. PIXELSHADER_PARAMETER g_PixelShaderParameters[] =
  226. {
  227.     {   D3DS_SR0,  "r0", "Register 0" },
  228.     {   D3DS_SR1,  "r1", "Register 1" },
  229.     {   D3DS_SR2,  "r2", "Register 2" },
  230.     {   D3DS_SR3,  "r3", "Register 3" },
  231.     {   D3DS_SR4,  "r4", "Register 4" },
  232.     {   D3DS_SR5,  "r5", "Register 5" },
  233.     {   D3DS_SR6,  "r6", "Register 6" },
  234.     {   D3DS_SR7,  "r7", "Register 7" },
  235.     {   D3DS_T0,   "t0", "Texture 0" },
  236.     {   D3DS_T1,   "t1", "Texture 1" },
  237.     {   D3DS_T2,   "t2", "Texture 2" },
  238.     {   D3DS_T3,   "t3", "Texture 3" },
  239.     {   D3DS_T4,   "t4", "Texture 4" },
  240.     {   D3DS_T5,   "t5", "Texture 5" },
  241.     {   D3DS_T6,   "t6", "Texture 6" },
  242.     {   D3DS_T7,   "t7", "Texture 7" },
  243.     {   D3DS_C0,   "c0", "Constant 0" },
  244.     {   D3DS_C1,   "c1", "Constant 1" },
  245.     {   D3DS_C2,   "c2", "Constant 2" },
  246.     {   D3DS_C3,   "c3", "Constant 3" },
  247.     {   D3DS_C4,   "c4", "Constant 4" },
  248.     {   D3DS_C5,   "c5", "Constant 5" },
  249.     {   D3DS_C6,   "c6", "Constant 6" },
  250.     {   D3DS_C7,   "c7", "Constant 7" },
  251.     {   D3DS_V0,   "v0", "Vertex color 0" },
  252.     {   D3DS_V1,   "v1", "Vertex color 1" },
  253.     {   D3DS_V1,   NULL, NULL },
  254. };
  255.  
  256.  
  257.  
  258. // Define some preset pixel shaders
  259. TCHAR* g_strPixelShaderPresets[10] =
  260. {
  261.     {
  262.         "// Diffuse only\r\n"
  263.         "ps.1.0\r\n"
  264.         "mov r0, v0"
  265.     },
  266.     {
  267.         "// Decal\r\n"
  268.         "ps.1.0\r\n"
  269.         "tex t0\r\n"
  270.         "mov r0, t0"
  271.     },
  272.     {
  273.         "// Modulate texture with diffuse (color0)\r\n"
  274.         "ps.1.0\r\n"
  275.         "tex t0\r\n"
  276.         "mul r0, v0, t0"
  277.     },
  278.     {
  279.         "// Blend between tex0 and tex1\r\n"
  280.         "ps.1.0\r\n"
  281.         "tex t0\r\n"
  282.         "tex t1\r\n"
  283.         "mov r1, t1\r\n"
  284.         "lrp r0, v0, t0, r1"
  285.     },
  286.     {
  287.         "// Scale tex0 by color0 and add to tex1\r\n"
  288.         "ps.1.0\r\n"
  289.         "tex t0\r\n"
  290.         "tex t1\r\n"
  291.         "mov r1, t1\r\n"
  292.         "mad r0, v0, t0, r1"
  293.     },
  294.     {
  295.         "// Add tex0, tex1, color0, and color1\r\n"
  296.         "ps.1.0\r\n"
  297.         "tex t0\r\n"
  298.         "tex t1\r\n"
  299.         "add r0, t0, v1\r\n"
  300.         "add r0, r0, t1\r\n"
  301.         "add r0, r0, v0"
  302.     },
  303. };
  304.  
  305.  
  306.  
  307.  
  308.  
  309. #endif
  310.  
  311.  
  312.